package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exceptions.BusinessException;
import com.tanhua.server.intercepror.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentsService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private MqMessageService mqMessageService;

    //分页查询指定动态的评论
    public PageResult findComments(Integer page, Integer pagesize, String movementId) {
        Integer commentType = 2;//互动类型：评论
        //调用API查询评论数据
        List<Comment> list = commentApi.findComments(page, pagesize, commentType, movementId);
        //判断评论列表是否存在，返回空
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //提取所有发布人的id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds);
        //查询所有发布人信息
        //遍历List<Comment>，一个Comment构造一个vo
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            //获取评论人的用户id
            Long userId = comment.getUserId();
            //根据key值userId查询出UserInfo
            UserInfo userInfo = map.get(userId);
            //把用户信息和互动表中的评论信息构造成一个vo
            CommentVo vo = CommentVo.init(userInfo, comment);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    //发布评论
    public void saveComment(String movementId, String content) {
        //判断用户是否冻结，查出用户
        Long userId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId);
        //String userStatus = userInfo.getUserStatus();
        if (userInfo.getUserStatus().equals("2")) {//用户状态,1为正常，2为冻结
            Integer freezingRange = userInfo.getFreezingRange();
            if (freezingRange == 2) {//冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
                throw new BusinessException(ErrorResult.myError("该账号被冻结"));
            }
        }
        //构造Comment对象
        Integer commentType = 2;
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(commentType);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());
        //调用API保存评论数据，返回评论总数量
        Integer count = commentApi.save(comment,2);
        mqMessageService.sendLogMessage(UserHolder.getUserId(),"0205","log.movement",movementId);

        System.out.println("评论总数量："+count);
    }
}
